﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.IO.Compression;
using Weather;
using Xmlprocess;

namespace Labcyk30
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        string key = Properties.Resources.key;
        string location;
        string uri = "https;//devapi.qweather.com/v7/weather/now?location=";
        public string Getjson(string uri)
        {
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(uri);
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            Stream stream = httpResponse .GetResponseStream();
            StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress), Encoding.GetEncoding("UTF-8"));
            string result = reader.ReadToEnd();
            stream.Close();
            return result;
        }
        private void BindData()
        {
            Dictionary<string, string> dicItem = new Dictionary<string, string>();
            dicItem.Add("101200101", "武汉");
            dicItem.Add("101010100", "北京");
            dicItem.Add("101020100", "上海");
            dicItem.Add("101280601", "深圳");
            dicItem.Add("101280101", "广州");
            cmb.ItemsSource = dicItem;
            cmb.SelectedIndex = 0;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            BindData();
            txturi.Text = uri;
        }

        private void btCon_Click(object sender, RoutedEventArgs e)
        {
            msg.Text = Getjson(txturi.Text);
        }
       

        private void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            location = cmb.SelectedValue.ToString();
            uri = "https://devapi.qweather.com/v7/weather/now?location=";
            uri = uri + location + "&key=" + key;
            string strjson = Getjson(uri);
            Xmlsd x = new Xmlsd();
            Rootobject r = new Rootobject();
            r = x.deserizlizejson<Weather.Rootobject>(strjson);
            weatherinfo.Text = r.ToString();
            txturi.Text = uri;
        }
    }
}
